package com.dzsw.javabase.regx;

import com.dzsw.javabase.regx.sqllike.LikeClauseExtractor;
import com.dzsw.javabase.regx.sqllike.LikeCondition;
import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.expression.Expression;
import net.sf.jsqlparser.parser.CCJSqlParserUtil;
import net.sf.jsqlparser.statement.select.PlainSelect;
import net.sf.jsqlparser.statement.select.Select;

import java.io.StringReader;
import java.util.regex.Pattern;
import java.util.List;

public class LikeSample {

    public static void main(String[] args) throws JSQLParserException {

        String reg = "\\bLIKE\\b.*\\#\\{\\b.*\\}(?!\\s+\\bescape)";
        String regx = "\\bLIKE\\s+\\#\\{\\b.*\\}(?!\\s+escape)";
        Pattern pattern = Pattern.compile(regx, Pattern.CASE_INSENSITIVE);

        String sql = "Select * from user where name like #{param.code} escape '\\' name like #{p.s} and name like #{p.s}";
        String sql1 = "Select * from user where name like #{param.code} escape '\\' and code like like #{p.s}";
        String sql2 = "select * form (Select * from user where name like ? escape '\\\\' and code like ?) tmp where 1 = 1";
        String sql3 = "SELECT *\n" +
                "FROM (SELECT TMP.*, ROWNUM ROW_ID\n" +
                "      FROM (SELECT CODE code, CODE DEFAULT_VALUE_0 FROM FORMAT WHERE NAME = 'SALESNOTE' GROUP BY CODE ORDER BY CODE) TMP\n" +
                "      WHERE ROWNUM <= 3)\n" +
                "WHERE ROW_ID > 0 AND CODE LIKE ?;";
        test(sql);
        test(sql1);
        test(sql2);
        test(sql3);


    }

    public static void test(String sql) throws JSQLParserException {
        List<LikeCondition> list = LikeClauseExtractor.extractLikeClauses(sql);
        for (LikeCondition c: list) {
            System.out.println(c.toString());
        }
        System.out.println("---------------------------------------------------------");
    }
}
